package com.liyujie.networkrequest.ui.home;

import android.text.TextUtils;

import androidx.databinding.ObservableField;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.liyujie.networkrequest.common.Constants;
import com.liyujie.networkrequest.listener.BeCommonCallback;
import com.liyujie.networkrequest.mobile.entity.SignatureDtoEntity;
import com.liyujie.networkrequest.remotedata.InterceptObserver;
import com.liyujie.networkrequest.ui.management.BackstageActivity;
import com.liyujie.networkrequest.utils.InitializeUtils;
import com.liyujie.networkrequest.utils.open.OpenCabinetUtils;
import com.liyujie.networkrequest.utils.popup.PopupUtils;
import com.liyujie.networkrequest.views.passlibrary.PayPassDialog;
import com.liyujie.networkrequest.views.passlibrary.PayPassView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomeViewModel  extends ViewModel implements PayPassView.OnPayClickListener{

    /**
     * 点击状态
     */
    private ObservableField<Integer> clickStatus = new ObservableField<>(0);

    /**
     * 内容
     */
    private ObservableField<String> contentStatus = new ObservableField<>("");



    /**
     * 登陆
     */
    private PayPassDialog payPassDialog;
    /**
     * 弹框显示
     */
    public void showDialog(){
        payPassDialog = new PayPassDialog(ActivityUtils.getTopActivity());
        payPassDialog.getPayViewPass().setPayClickListener(this);
        if(null != payPassDialog){
            payPassDialog.show();
        }
    }

    @Override
    public void onPassFinish(String password) {
        //6位输入完成回调
        String managementPassword = SPUtils.getInstance().getString("managementPassword","123456");
        if(managementPassword.equals(password)){
            ActivityUtils.startActivity(BackstageActivity.class);
            payPassDialog.dismiss();
        }else {
            InitializeUtils.getDefaultMaker().show("密码错误");
        }
    }

    @Override
    public void onPayClose() {
        //关闭回调
        payPassDialog.dismiss();
    }




    /**
     * 密码开箱
     */
    public void PasswordUnpacking(){
        /**
         * 密码
         */
        clickStatus.set(0);
        /**
         *
         */
        PayPassDialog mPayPassDialog = new PayPassDialog(ActivityUtils.getTopActivity());
        mPayPassDialog.getPayViewPass().setPayClickListener(new PayPassView.OnPayClickListener() {
            @Override
            public void onPassFinish(String password) {
                /**
                 * 关闭
                 */
                mPayPassDialog.dismiss();
                /**
                 * 添加密码
                 */
                contentStatus.set(password);
                /**
                 * 验证接口
                 */
                SignatureDto();
            }

            @Override
            public void onPayClose() {
                //关闭回调
                mPayPassDialog.dismiss();
            }
        });
        if(null != mPayPassDialog){
            mPayPassDialog.show();
        }
    }

    /**
     * 刷卡开箱
     */
    public void scanCardOpen(){

        /**
         * 刷卡
         */
        clickStatus.set(1);
        /**
         * 刷卡内容
         */
        PopupUtils.showSwipeCardPopup(ActivityUtils.getTopActivity(), new BeCommonCallback() {
            @Override
            public void onBeCommonCallback(Object mObject) {
                String SwipeCard = (String) mObject;
                if(!TextUtils.isEmpty(SwipeCard)){
                    /**
                     * 刷卡内容
                     */
                    contentStatus.set(SwipeCard);
                    /**
                     * 验证接口
                     */
                    SignatureDto();
                }
            }
        });

    }

    /**
     * 扫码开箱
     */
    public void scanCodeOpen(){
        /**
         * 扫码
         */
        clickStatus.set(2);
        /**
         * 扫码内容
         */
        PopupUtils.showScanQRCodesPopup(ActivityUtils.getTopActivity(), new BeCommonCallback() {
            @Override
            public void onBeCommonCallback(Object mObject) {
                String scanQRCodes = (String) mObject;
                if(!TextUtils.isEmpty(scanQRCodes)){
                    /**
                     * 扫码内容
                     */
                    contentStatus.set(scanQRCodes);
                    /**
                     * 验证接口
                     */
                    SignatureDto();
                }
            }
        });
    }




    /**
     * 验证接口
     */
    public void SignatureDto(){
        /**
         * 正在请求服务
         */
        PopupUtils.showAwaitTipsPopup(ActivityUtils.getTopActivity(),"正在请求服务");
        /**
         * 传参
         */
        Map<String,Object> loginMessage = new HashMap<>();
        /**
         * -发出时间
         */
        loginMessage.put("dateTime",TimeUtils.getNowMills());
        /**
         * 设备信息
         */
        loginMessage.put("facility", DeviceUtils.getAndroidID());
        /**
         * 验证类型：Integer 0/1/2 密码/刷卡/扫码
         */
        loginMessage.put("type",clickStatus.get());
        /**
         * value-验证值：String  密码或者卡号或者扫码
         */
        loginMessage.put("value",contentStatus.get());//true成功  false失败
        /**
         *  发生内容
         */
        System.out.println(GsonUtils.toJson(loginMessage));
        /**
         *  验证接口
         */
        Observable<SignatureDtoEntity> mObservable = InitializeUtils.getAppComponent().getSystemService().signature(loginMessage);
        mObservable.subscribeOn(Schedulers.io()).subscribe(new Observer<SignatureDtoEntity>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(SignatureDtoEntity signatureDtoEntity) {
                System.out.println("接受数据" + GsonUtils.toJson(signatureDtoEntity));
                /**
                 * 开启柜子
                 */
                OpenCabinetUtils.OpenTheCabinet(signatureDtoEntity);
            }

            @Override
            public void onError(Throwable e) {
                InitializeUtils.getDefaultMaker().show("请求出错或网络有问题" + e.getMessage());
                /**
                 * 关闭弹框
                 */
                PopupUtils.dismissAwaitTipsPopup();
            }

            @Override
            public void onComplete() {
                /**
                 * 关闭弹框
                 */
                PopupUtils.dismissAwaitTipsPopup();
            }
        });
    }
}
